$(function() {

    // 访问页面 光标自动定位到用户名输入框
    $('#name').focus();

    var notice = '尊敬的用户您好，本系统于2018年1月18日下午14至17点整进行日常维护升级，期间不会影响您的使用，祝生活愉快！';
    // notice = ''; // 无消息展示情况
    // notice += notice; // 消息超过边框展示情况
    // 系统公告
    if (notice) {
        var i = 1,
            interval = 0,
            speed = 500,
            $notice = $('#notice'),
            $noticeBox = $('#notice-box'),
            noticeBoxHeight = $noticeBox.height(),
            $noticeBoxChildren = $noticeBox.children('div'),
            $noticeFirst = $noticeBoxChildren.first(),
            $noticeLast = $noticeBoxChildren.last()

        ;

        $notice.show();
        $noticeBoxChildren.html(notice);
        var noticeHeight = $noticeFirst.height();

        var _doscroll = function() {
            $noticeBox.animate(
                10,
                function() {
                    $noticeBox.scrollTop(i++);
                    if (i >= noticeHeight) {
                        i = 1;
                        $noticeFirst.appendTo($noticeBox);
                    }
                }
            );
        };

        // 文本内容高度超过了设定边框
        if (noticeHeight > noticeBoxHeight) {
            var _scorll = function(speed) {
                interval = setInterval(_doscroll, speed);
            };
            setTimeout(function() {
                _scorll(speed);
            }, 2000);

            $noticeBox.on('mousemove', function() {
                clearInterval(interval);
            }).on('mouseout', function() {
                _scorll(speed);
            });
        } else {
            $noticeLast.hide();
        }
    }

    // Android、IOS下载图标效果
    $('#an,#ios').on('mousemove', function() {
        $(this).attr('src', $(this).attr('other'));
    }).on('mouseout', function() {
        $(this).attr('src', $(this).attr('default'));
    }).trigger('mouseout');


    var $loginTips = $('#login-tips');
    // 显示登录提示错误消息
    var _showErrTips = function(msg) {
        $loginTips.html(msg);
        $loginTips.show();
        $loginTips.next('div').css('margin-top', '13px');
    };

    // 隐藏登录提示错误消息
    var _hideErrTips = function() {
        $loginTips.next('div').css('margin-top', '40px');
        $loginTips.hide();
    };
    _hideErrTips();

    // login
    $('#submit').on('click', function() {

        var $name = $('#name'),
            nameVal = $name.val(),
            $password = $('#password'),
            passwordVal = $password.val();

        if (!nameVal && !passwordVal) {
            _showErrTips('请输入账号和密码');
            return false;
        }

        if (!nameVal) {
            _showErrTips('请输入账号');
            return false;
        }

        if (!passwordVal) {
            _showErrTips('请输入密码');
            return false;
        }

        // 用户名密码正确性校验
        if (nameVal != 1 || passwordVal != 1) {
            _showErrTips('用户名或密码错误，请重新输入');
            return false;
        }

    });


    //   是否支持 placeholder
    var _placeholderSupport = function placeholderSupport() {
        return 'placeholder' in document.createElement('input');
    };

    //  placeholder 兼容
    if (!_placeholderSupport()) {

        // trim 兼容性调整
        if (!String.prototype.trim) {
            String.prototype.trim = function() {
                return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
            };
        }

        var _show = function(obj) {
            obj.css('visibility', 'visible');
        };

        var _hide = function(obj) {
            obj.css('visibility', 'hidden');
        };

        //默认遍历循环添加placeholder
        var $placeholderInput = $('[placeholder]');
        $placeholderInput.each(function() {
            var $this = $(this),
                placeholderDivHtml = "<div class='placeholder'><span>" + $this.attr('placeholder') + "</span></div>";

            $this.after(placeholderDivHtml);
        });
        // placeholder 显示隐藏控制
        $placeholderInput.on('keyup keypress', function(e) {
            var $this = $(this);
            $this.trigger('focus');
        }).on('focus blur', function() {
            var $this = $(this),
                $div = $this.next("div")
            thisVal =  $this.val().trim();
            if (thisVal == '') {
                _show($div);
            } else {
                _hide($div);
            }
        });

        $('.placeholder').on('click', function() {
            $(this).prev('[placeholder]').trigger('focus');
        });
    }
});